<?php

namespace App\Models\Ddaman;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use App\Models\common\Base;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\DB;

class Menu extends Base
{
    use HasFactory,SoftDeletes;

    protected $table = "sy_function";

    const CREATED_AT = null;
    const UPDATED_AT = null;

    protected $fillable = [
        'name','pid','code','father_code','platform','target','stop','sort','url','component','display','icon_class'
    ];


    public $rule = [
        'name' => 'required|string|between:2,100',
        //'code' => 'required|unique:function',
    ];

    public $editRule = [
        'id' => 'required',
        'name' => 'required|string|between:2,100',
    ];

    public $message = [
        'id.required' => '标识必须',
        'code.required' => '标识必须填写',
        'code.unique' => '标识不能重复',
        'name.required' => '名称必须填写',
    ];

    /*
     * 获取列表
     * */
    public function getList( $param = [] )
    {
        $limit = isset($param['limit']) ? $param['limit'] : 2;
        $where = [];

        if (isset($param['keywords']) && $param['keywords']){
            $keywords = trim($param['keywords']);
            $where[] = ['name','like',"%$keywords%"];
        }
        $type = isset($param['type']) ? (int)$param['type'] : 1;

        $menu = $this->where('target',$type)->get()->toArray();
        $list = list_to_tree($menu);

        return $list;

    }


    /*
     * 获取我的权限
     * */
    public function getRule()
    {
        //我的权限
        $role = Role::where('id',get_role())->first();
        $menu = [];
        $api = [];
        if ($role){

            $menuIds = json_decode($role->menu_rule,true);
            $apiIds = json_decode($role->api_rule,true);
            if ($menuIds){
                $menu = $this->getRuleList($menuIds,1);
            }
            if ($apiIds){
                $api = $this->getRuleList($apiIds,2);
            }
        }
        //dump($role);die;

        $data = [
            //'menu_id'=>$menuIds,
            'menu'=>$menu,
            'api'=>$api
        ];
        return $data;
    }

    /*
     * 获取权限列表
     * */
    private function getRuleList( $ids ,$type = 1)
    {
        $where = [
            ['display',0]
        ];
        $list = [];
        switch ($type){
            case 1:
                /*$list = $this->whereIn('id',$ids)->where($where)
                    ->select([
                        'id','name','pid','component','sort','icon_class'
                    ])
                    ->orderBy('sort','asc')
                    ->get()
                    ->toArray();
                $list = list_to_tree($list,'id','pid');*/
                $list = DB::table('sy_function as t')
                    ->leftJoin('sy_function as c','t.pid','=','c.id')
                    ->whereIn('t.id',$ids)
                    ->where([
                        ['t.display',0]
                    ])
                    ->select([
                        't.id','t.name','t.pid','t.component','t.sort as t_sort','t.icon_class',
                        'c.id as c_id','c.name as c_name','c.pid as c_pid','c.component as c_component','c.sort','c.icon_class as c_icon_class',
                    ])
                    ->orderBy('t.sort','asc')
                    ->get()
                    ->toArray();

                $menu2 = [];
                foreach ($list as $key => $item){
                    $item =  json_decode( json_encode( $item),true);
                    $keyName = $item['pid'];
                    if ($item['pid'] === 0){
                        $keyName = $item['id'];
                    }
                    if (!isset($menu2[$keyName])){
                        if ($item['pid'] === 0){
                            $top = [
                                'id'=>$item['id'],
                                'sort'=>$item['sort'],
                                'name'=>$item['name'],
                                'icon_class'=>$item['icon_class'],
                                'component'=>$item['component'],
                                'identify'=>'top',
                            ];
                            $menu2[$keyName] = $top;
                        }else{
                            $top = [
                                'id'=>$item['c_id'],
                                'sort'=>$item['sort'],
                                'name'=>$item['c_name'],
                                'icon_class'=>$item['c_icon_class'],
                                'component'=>$item['c_component'],
                                'identify'=>'top',
                            ];
                            $menu2[$keyName] = $top;
                        }

                    }

                    //dump($menu2);die;
                    if ($menu2[$keyName]){
                        $children = [
                            'id'=>$item['id'],
                            'sort'=>$item['t_sort'],
                            'name'=>$item['name'],
                            'icon_class'=>$item['icon_class'],
                            'component'=>$item['component'],
                            'identify'=>'bottom',
                        ];

                        $menuKey = $item['id'];
                        if ($item['pid']){
                            $menuKey = $item['pid'];
                        }

                        if ($item['pid'] > 0 ){
                            $menu2[$keyName]['children'][] = $children;
                        }else if ($menu2[$keyName]['id'] != $menuKey  ){
                            $menu2[$menuKey] = $children;
                        }
                    }
                }
                //dump($menu2);die;
                // 取得列的列表
                foreach ($menu2 as $key => $row)
                {
                    $sort[$key]  = $row['sort'];
                }
                array_multisort($sort, SORT_ASC,$menu2);
                $list = $menu2;
                break;
            case 2:
                $list = $this->whereIn('id',$ids)->where($where)
                    ->select([
                        'id','name','pid','component','sort','url'
                    ])
                    ->orderBy('sort','asc')
                    ->get()
                    ->toArray();
                break;
        }

        return $list;
    }



}
